<template>
  <div class="layout-parent w100 p10">
    <el-tabs type="border-card">
      <el-tab-pane>
        <template #label><vp-label zh="基本信息" en="Basic Info"/></template>
        <VoluntaryDept
          :id="businessId"
          :type="'feedback'"
          @dataEmits="getData"
          ref="orgHandleRef"
        ></VoluntaryDept>
      </el-tab-pane>
      <el-tab-pane>
        <template #label><vp-label zh="流程记录" en="Process Record"/></template>
        <ProcessHistory :processInsId="processInsId" />
      </el-tab-pane>
      <el-tab-pane>
        <template #label><vp-label zh="流程图" en="Process Chart"/></template>
        <ProcessView :processInsId="processInsId" />
      </el-tab-pane>
      <el-tab-pane>
        <template #label><vp-label zh="操作记录" en="Operation Record"/></template>
        <RcList :id="formData.id" ref="rcListRef"></RcList>
      </el-tab-pane>
    </el-tabs>
    <StatusBar
      :id="formData.id"
      :status="formData.zt"
      :isValid="formData.yxbs"
    />
    <PageBottom>
      <VpProcessToolBar 
        :process-ins-id="processInsId" 
        :task-id="taskId" 
        :loading="submitLoading" 
        :before-show-fn="beforeShowFn"
        :after-agree-fn="afterAgreeFn"
        :set-loading-fn="(_loading: boolean) => { submitLoading = _loading }"
        :show-reject="false"
      />
    </PageBottom>
  </div>
</template>
<script lang="ts">
import { defineComponent,  getCurrentInstance,  reactive, ref, toRefs } from "vue";
import { useI18n } from "vue-i18n";
import { useRoute, useRouter } from "vue-router";
import VoluntaryDept from "./VoluntaryDept.vue";
import RcList from "/@/components/vp/operationRecord/VpOperationRecordCard.vue";
import ProcessHistory from '/@/components/vp/workflow/ProcessHistory.vue';
import ProcessView from "/@/components/vp/workflow/ProcessView.vue";
import request from "/@/utils/request";
import { ElMessage, ElMessageBox } from "element-plus";
import PageBottom from '/@/components/vp/PageBottom/index.vue';
import mittBus from '/@/utils/mitt';
import StatusBar from '/@/components/vp/status/StatusBar.vue';
import VpProcessToolBar from '/@/components/vp/workflow/VpProcessToolBar.vue';

export default defineComponent({
  name: "auditVoluntaryDept4Measure",
  props: {
    businessId: String,
    taskId: String,
    processInsId: String,
  },
  components: {
    RcList,
    VoluntaryDept,
    ProcessHistory,
    ProcessView,
    PageBottom,
    StatusBar, 
    VpProcessToolBar
  },
  emits: ["formSubmited"],
  setup(props, context) {
    const state = reactive({
      type: "measure",
      loading: false,
      submitLoading: false,
      formData: {
        id: "",
        zt: 10,
        yxbs: 1,
        processid: "",
        cnbs: null,
        cnsm: "",
        clrid: "",
        clrq: "",
        cljg: "",
        clr: "",
      },
    });

    const editDialogRef = ref();
    const router = useRouter();
    const orgHandleRef = ref();
    const { t } = useI18n();
    const { proxy } = getCurrentInstance() as any;
    const route = useRoute();

    const getData = (data: any) => {
      state.formData = data;
    };

    //同意前操作
    const beforeShowFn = () => {
      state.submitLoading = true;
      return orgHandleRef.value.getDeptData(20).finally(()=>{state.submitLoading = false;});
    }
    
    //同意后操作
    const afterAgreeFn = (processHandle: any) => { 
      state.submitLoading = true;
      orgHandleRef.value.getDeptData(20).then((data: any) =>{
        request.post("/voluntary/dept/deptHandle/feedback", {
            ...data,
            processHandle: {
                processInsId: props.processInsId,
                taskId: props.taskId,
                ...processHandle,
            }
        }).then(()=>{
            mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
            ElMessage.success("办理完毕/Completed Processing");
        }).catch(()=>{
        }).finally(()=>{
            state.submitLoading = false;
        })
      }).catch(()=>{state.submitLoading = false;});
    }

    return {
      ...toRefs(state),
      ...toRefs(props),
      editDialogRef,
      orgHandleRef,
      getData,
      beforeShowFn,
      afterAgreeFn,
    };
  },
});
</script>

<style lang="scss" scoped>
.drawer {
  :deep(.el-checkbox__label) {
    min-width: 80px;
  }
}
</style>
